//
//  MusicViewModel.swift
//  NDAi
//
//  Created by 丸子 on 2024/7/26.
//

import UIKit

class MusicViewModel {
    let reloadHomeDataSubject = PublishSubject<[MusicModel]>()
    let reloadListSubject = PublishSubject<Void>()
    let createSuccessSubject = PublishSubject<String>()
    var lyric_desc:String?
    var page = 1
    let reloadUserListDataSubject = PublishSubject<[MusicModel]>()

    func getHomeList(){
        NDProgressHUD.showLoading()
        RequestService.instance.music_homelist { models in
            NDProgressHUD.hide()
            self.reloadHomeDataSubject.onNext(models)
        } failureClosure: { error in
            NDProgressHUD.show(message: error ?? "")
        }
    }
    
    func generateDesc(){
        guard let desc = lyric_desc else {
            NDProgressHUD.show(message: "请输入描述后再优化")
            return
        }
        kHideKeyboard()
        NDProgressHUD.showLoading()
        RequestService.instance.createLyric_desc(lyric_desc: desc) { desc in
            NDProgressHUD.hide()
            self.lyric_desc = desc
            self.reloadListSubject.onNext(())
        } failureClosure: { error in
            NDProgressHUD.show(message: error ?? "")
        }
    }
    
    
    func musicCreate(){
        guard let desc = lyric_desc else {
            NDProgressHUD.show(message: "请输入歌曲描述")
            return
        }
        NDProgressHUD.showLoading()
        RequestService.instance.music_create(name: "", type: 1, lyrics: "", lyric_desc: desc, style_text: "", is_instrumental: 0) { checkId in
            NDProgressHUD.hide()
            self.createSuccessSubject.onNext(checkId)
        } failureClosure: { error in
            NDProgressHUD.show(message: error ?? "")
        }

    }
    
    
    func userList(){
        NDProgressHUD.showLoading()
        RequestService.instance.UserMusicList(page: page) { models in
            NDProgressHUD.hide()
            self.reloadUserListDataSubject.onNext(models)
        } failureClosure: { error in
            NDProgressHUD.show(message: error ?? "")
        }
    }
    
    func deleteMusic(id:String){
        NDProgressHUD.showLoading()
        RequestService.instance.UserDeleteMusic(id: id) {[weak self] models in
            NDProgressHUD.show(message: "删除成功")
            self?.userList()
        } failureClosure: { error in
            NDProgressHUD.show(message: error ?? "")
        }
    }
}
